package com.xy.smartsms.service.carrierparam;

import android.content.Context;
import android.os.Build;

import com.xy.smartsms.db.carrierparam.OnlineConfigManager;
import com.xy.smartsms.db.carrierparam.entity.OnlineConfigItem;
import com.xy.smartsms.constant.NetConstant;
import com.xy.smartsms.util.Log;
import com.xy.smartsms.util.ThreadManager;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.util.BaseRunnable;


/**
 * Created by kangwenbo on 2017/3/1.
 */

public class UpdateOnlineConfigRunnable extends BaseRunnable {

    private final String TAG = "UpdateOnlineConfigRunnable";
    private Context mContext;
    private Map<String, Object> queryMap;

    private String mSchemeVersion;
    private String mCNum;
    private String mIccid;

    private Map<String, Object> getPhoneInfo(Context context) {
        queryMap = new HashMap<String, Object>();
        queryMap.put("phoneModel", Build.BRAND + " " + Build.MODEL);
        queryMap.put("androidVersion", Build.VERSION.RELEASE);
        queryMap.put("phoneOsVersion", Build.VERSION.INCREMENTAL);

        return queryMap;
    }

    /**
     * @param schemeVersion 融合方案版本
     * @param cnum  短信中心号
     * @param iccid
     * @return
     */
    public void requestData(Context context, String schemeVersion, String cnum,String iccid) {
        mContext = context;
        mSchemeVersion = schemeVersion;
        mCNum = cnum;
        mIccid = iccid;
        ThreadManager.executeRunnableByPool(ThreadManager.NUMBER_UPDATE_CARRIER, this);
    }

    @Override
    protected boolean inQueryInterval() {
        if (OnlineConfigManager.getInstance().getIntervalTime(OnlineConfigItem.FLAG_CONFIG_LIST_LAST_VERSION) < System.currentTimeMillis())
            return false;
        return true;
    }

    private void initData() {
        queryMap = getPhoneInfo(mContext);
        queryMap.put("version", OnlineConfigManager.getInstance().getMaxVersion());
        queryMap.put("schemeVersion", mSchemeVersion);
        queryMap.put("iccid", mIccid);
        queryMap.put("cnum", mCNum);
    }

    @Override
    public void execute() {
        initData();
        UpdateCarrierParamService.updateDataByNet(NetConstant.REQUEST_URL_DIRECTIVE, queryMap, new HashMap<String, String>(), new XyCallBack() {
            @Override
            public void execute(Object... objects) {
                try {
                    JSONObject jb = UpdateCarrierParamService.isValidOfData(objects);
                    if (jb == null) {
                        UpdateCarrierParamService.updateOnlineConfig(OnlineConfigItem.FLAG_CONFIG_LIST_LAST_VERSION, OnlineConfigItem.STATUS_FAIL);
                        return;
                    }
                    analysisData(jb);
                    UpdateCarrierParamService.updateOnlineConfig(OnlineConfigItem.FLAG_CONFIG_LIST_LAST_VERSION, OnlineConfigItem.STATUS_SUCCESS);
                } catch (Throwable t) {
                    Log.e(TAG, "execute",t);
                    UpdateCarrierParamService.updateOnlineConfig(OnlineConfigItem.FLAG_CONFIG_LIST_LAST_VERSION, OnlineConfigItem.STATUS_FAIL);
                }
            }
        });
    }

    private void analysisData(JSONObject jsonObject) {
        try{
            if (jsonObject.has("datas")) {
                JSONArray jsonArray = jsonObject.optJSONArray("datas");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jb = jsonArray.optJSONObject(i);
                    OnlineConfigItem onlineConfigItem = new OnlineConfigItem();
                    onlineConfigItem.setKey(jb.optString("key"));
                    onlineConfigItem.setValue(jb.optString("value"));
                    onlineConfigItem.setVersion(jb.optString("version"));
//                onlineConfigItem.setLastUpdateTime(System.currentTimeMillis());
                    OnlineConfigManager.getInstance().updateOrInsert(onlineConfigItem, onlineConfigItem.getContentValues());
                }
            }
        }catch (Throwable t){
            Log.e(TAG,"analysisData",t);
        }

    }

}
